// perdocinfo/index.js
var app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [], //多选框
    mask_key: true,
    textarea: '',
    title:['请填写疾病名称或症状',
           '选择病程',
           '最近去医院就诊过吗',
           '现在的病情变化情况',
           '病情主诉',
           '图片材料'],
    raditem1: [{
      name: '一月内',
        value: '一月内',
        checked: false
      },
      {
        name: '一周内',
        value: '一周内',
        checked: false
      },
      {
        name: '半年内',
        value: '半年内',
        checked: false
      },
      {
        name: '大于半年',
        value: '大于半年',
        checked: false
      }
    ], //单选框
    raditem2:[
      {
        name:'没有就诊过',
        value:'没有就诊过',
        checked: false
      },
      {
        name: '就诊过',
        value: '就诊过',
        checked: false
      }
    ],
    raditem3: [{
        name: '基本稳定',
        value: '基本稳定',
        checked: false
      },
      {
        name: '基本痊愈',
        value: '基本痊愈',
        checked: false
      },
      {
        name: '急性发作期',
        value: '急性发作期',
        checked: false
      }
    ], //单选框
    img_arr: [],//图片材料
    data_arr:[],
    two_input_key: false,
    all_key: false,
    hospital:'',//就诊医院名称
    department:'',//就诊科室名称
    inputtext:'',//病情主诉
    radiodata1:'',//单选框1
    radiodata2: '',//单选框2
    radiodata3: '',//单选框3
    trantitle:['请填写疾病名称或症状','选择病程','最近去医院就诊过吗？','现在的病情变化情况','病情主诉','图片材料'],
    trancont:[],
    tranprev:[]
  },
  // 多选框
  checkboxChange: function(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    wx.setStorageSync('jbname', e.detail.value)
  },
  // 删除数据
  deldata: function(e) {
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    this.data.items.splice(index, 1)
    this.setData({
      items: this.data.items
    })
    wx.setStorageSync('jbname', this.data.items)
  },
  //显示遮罩层 
  showpop: function() {
    this.setData({
      textarea: "",
      mask_key: false,
      all_key: true
    })
  },
  //获取文本框内容 
  getext: function(e) {
    this.setData({
      textarea: e.detail.value
    })
  },
  // 添加新疾病
  addinfo: function(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      mask_key: true,
      all_key: false
    })
    for (var i = 0; i < this.data.items.length; i++) {
      if (this.data.textarea == this.data.items[i].value) {
        wx.showToast({
          title: '请勿重复添加',
          icon: "none"
        })
        return false
      }
    }
    if (index == 1 && this.data.textarea) {
      let obj = {
        name: this.data.textarea,
        value: this.data.textarea,
        checked: true
      }
      this.data.items.push(obj)
      this.setData({
        items: this.data.items
      })
      wx.setStorageSync('jbname', this.data.items)
    }
  },
  // 选择病程单选框
  radioChange: function(e) {    
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      radiodata1: e.detail.value
    })
  },

  // 医院就诊单选框
  radioChange2: function(e) {
    console.log('radio发生change事件，携带value值为：', e)
    if(e.detail.value=='没有就诊过'){
      this.setData({
        two_input_key:false
      })
    } else if (e.detail.value == '就诊过'){
      this.setData({
        two_input_key: true
      })
    }

    this.setData({
      radiodata2: e.detail.value
    })
  },
  
  //病情变化单选框
  radioChange3: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      radiodata3: e.detail.value
    })
  },
  // changeradio3: function (e) {
  //   let index = e.currentTarget.dataset.index;
  //   let check = `raditem3[${index}].checked`
  //   this.setData({
  //     [check]: !this.data.raditem3[index].checked
  //   })
  // },
  addimg: function() {
    let that = this;
    var uniacid = app.siteInfo.uniacid
    var data_arr = that.data.data_arr;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function (res) {
              console.log(res)
              data_arr.push(res.data)
              wx.setStorageSync('img_arr', data_arr)
              that.setData({
                img_arr: data_arr
              })
            }
          })
        }
      }
    })
  },
  delimg: function(e) {
    let index = e.currentTarget.dataset.index;
    this.data.img_arr.splice(index, 1)
    this.setData({
      img_arr: this.data.img_arr
    })
  },
  //医院
  hospital(e) {
    console.log(e)
    var valuearr = e.detail.value
    this.setData({
      hospital: e.detail.value
    })
  },
  //科室
  department(e) {
    var valuearr = e.detail.value
    this.setData({
      department: e.detail.value
    })
  },
  //输入病情
  inputtext(e) {
    var valuearr = e.detail.value
    this.setData({
      inputtext: e.detail.value
    })
  },

  //提交申请
  nextpage: function() {
    let obj0={
      question:'选择希望得到的帮助',
      anser: this.data.tranprev
    }
    let obj1={
      question: this.data.title[0],
      anser: this.data.items
    }
    let obj2 = {
      question: this.data.title[1],
      anser: this.data.radiodata1
    }
    let obj3 = {
      question: this.data.title[2],
      anser: this.data.radiodata2,
      input1: this.data.hospital,
      input2: this.data.department
    }
    let obj4 = {
      question: this.data.title[3],
      anser: this.data.radiodata3
    }
    let obj5 = {
      question: this.data.title[4],
      anser: this.data.inputtext
    }
    let obj6 = {
      question: this.data.title[5],
      anser: this.data.img_arr
    }

    if (this.data.items.length==0){
      wx.showToast({
        title: '请添加新疾病',
        icon:'none'
      })
    } else if (this.data.radiodata1==""){
      wx.showToast({
        title: '请选择病程',
        icon: 'none'
      })
    } else if (this.data.radiodata2 == "") {
      wx.showToast({
        title: '请补充信息',
        icon: 'none'
      })
    } else if (this.data.radiodata3 == "") {
      wx.showToast({
        title: '请选择病情变化情况',
        icon: 'none'
      })
    } else if (this.data.inputtext==""){
      wx.showToast({
        title: '请选择病情主诉',
        icon: 'none'
      }) 
    } else if (this.data.img_arr.length==0){
      wx.showToast({
        title: '请添加图片材料',
        icon: 'none'
      }) 
    }else{
      this.data.trancont.push(obj0, obj1, obj2, obj3, obj4, obj5, obj6)
      this.setData({
        trancont: this.data.trancont
      })

      let tranitem = wx.getStorageSync('items')
      for (var i = 0; i < tranitem.length; i++) {
        if (tranitem[i].checked == true) {
          let obj={
            value1: tranitem[i].value,
            value2: tranitem[i].textarea
          }
          this.data.tranprev.push(obj)
          this.setData({
            tranprev: this.data.tranprev
          })
        }
      }
      console.log('报告数组：',this.data.trancont)

      wx.navigateTo({
        url: '/hyb_yl/czhuanjiasubpages/pages/tijiandetail/index?num=3',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that= this
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        // console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})